<template>
	<!-- svip会员模块 -->
	<view class="vipPaid">
		<!-- #ifdef MP -->
		<NavBar titleText="SVIP会员" :iconColor="iconColor" :textColor="iconColor" :isScrolling="isScrolling" :showBack='showBack' :showHome='showHome'></NavBar>
		<!-- #endif -->
		<view class="headerBg"></view>
		<view class="card-section">
			<image src="../static/vip-paid.png" class="vip-paid"></image>
			<view class="wrapper">
				<view class="avatar-wrap acea-row">
					<image class="avatar" :src="userInfo.avatar"></image>
					<view class="text">
						<view class="name line1">{{userInfo.nickname}}</view>
						<view class="tips" v-if="userInfo.is_ever_level">永久SVIP会员</view>
						<view class="tips" v-else-if="userInfo.is_money_level">{{userInfo.overdue_time | dateFormat}} 到期</view>
						<view class="arrow" @click="goRightDetail">权益介绍<text class="iconfont icon-ic_rightarrow"></text></view>
					</view>
				</view>
				<view class="right-section">
					<view class="section-bd acea-row row-between-wrapper">
						<view v-for="item in memberRights" :key="item.id" class="item">
							<view class="image-wrap acea-row row-center row-middle">
								<view class="image-wrap-inner">
									<image class="image" :src="item.pic"></image>
								</view>
							</view>
							<view class="text">
								<view class="name">{{item.title}}</view>
								<view>{{item.explain}}</view>
							</view>
						</view>
					</view>
				</view>
			</view>
		</view>
		<!-- 选择卡种 -->
		<view class="other-section">
			<view class="type-section" id="card" v-if="memberType.length && !userInfo.is_ever_level">
				<view class="type-section-inner">
					<view class="title acea-row row-between-wrapper">
						<view class="">选择卡种</view>
						<view class="cash" @click="drawMemberCard">点击兑换卡密</view>
					</view>
					<scroll-view class="scroll" scroll-x="true">
						<view v-for="item in memberType" :key="item.id" class="item" :class="{on: item.id === svip.id}" @click="checkType(item)">
							<view v-if="item.is_label" class="badge">限时特惠</view>
							<view class="name">{{item.title}}</view>
							<view class="new">￥<text class="num">{{item.pre_price | moneyFormat}}</text></view>
							<view class="old">￥{{item.price | moneyFormat}}</view>
							<view class="button">立省{{ $util.$h.Sub(item.price, item.pre_price) }}元</view>
						</view>
					</scroll-view>
				</view>
			</view>
			<view v-if="memberCoupons.length" class="coupon-section">
				<view class="section-hd acea-row row-between-wrapper">
					<view class="">会员优惠券</view>
					<navigator class="link" url="/pages/annex/vip_coupon/index">查看更多<text class="iconfont icon-ic_rightarrow"></text></navigator>
				</view>
				<scroll-view class="section-bd" scroll-x="true">
					<view v-for="item in memberCoupons" :key="item.id" class="item acea-row row-center-wrapper" :class="{gray: item.is_fail || item.status === '已使用'}">
						<view class="inner">
							<view class="type">{{ item.type_name }}</view>
							<view class="money">
								<text v-if="item.coupon_type==1">￥</text>
								<text class="num">{{item.coupon_type == 1?item.coupon_price:parseFloat(item.coupon_price)/10}}</text>
								<text v-if="item.coupon_type==2">折</text>
							</view>
						</view>
						<view class="text">
							<!-- <view class="name">{{item.coupon_title}}</view> -->
							<view v-if="item.use_min_price == '0.00'" class="mark">无门槛券</view>
							<view v-else class="mark">满{{item.use_min_price | moneyFormat}}可用</view>
						</view>
						<!-- <view class="btn">{{item.status}}</view> -->
					</view>
				</scroll-view>
			</view>
			<view class="px-20">
				<recommend class="recommend" title="SVIP商品推荐" :hostProduct="goodsList" saleShow></recommend>
			</view>
		</view>
		<view class="footer">
			<view class="acea-row">
				<view class="text acea-row row-middle">
					<view><text class="symbol">¥</text><text class="number">{{ totalPrice }}</text><text class="original">原价¥{{ parseFloat(svip.price) }}</text></view>
				</view>
				<view class="buy acea-row row-center-wrapper" @tap="pay">{{ type == 'free' ? '立即试用' : '立即支付' }}</view>
			</view>
		</view>
		<view class="footer-placeholder"></view>
	</view>
</template>

<script>
	import colors from '@/mixins/color.js';
	import {
		mapGetters
	} from "vuex";
	import {
		memberCard,
		memberCardDraw,
		memberCardCreate,
		groomList,
		memberOverdueTime
	} from '@/api/user.js';
	import {
		orderOfflinePayType
	} from '@/api/order.js';
	import {
		toLogin
	} from '@/libs/login.js';
	import {
		openPaySubscribe
	} from '@/utils/SubscribeMessage.js';
	import dayjs from '@/plugin/dayjs/dayjs.min.js';
	import recommend from '@/components/recommend/index.vue';
	// #ifdef MP
	import NavBar from '@/components/NavBar';
	// #endif

	export default {
		components: {
			recommend,
			// #ifdef MP
			NavBar,
			// #endif
		},
		mixins: [colors],
		filters: {
			dateFormat: function(value) {
				return dayjs(value * 1000).format('YYYY-MM-DD');
			},
			moneyFormat: function(value) {
				return parseFloat(value);
			}
		},
		data() {
			return {
				memberType: [],
				userInfo: {},
				memberRights: [],
				memberExplain: [],
				memberCoupons: [],
				isGetFree: null,
				account: '',
				password: '',
				goodsList: [],
				pay_order_id: '',
				totalPrice: '0',
				page: 1,
				limit: 15,
				finished: false,
				memberEndTime: '',
				// #ifdef H5
				isWeixin: this.$wechat.isWeixin(),
				// #endif
				type: '',
				svip: {},
				isShowAuth: false,
				// #ifdef MP
				iconColor: '#FFFFFF',
				isScrolling: false,
				// #endif
				showBack: true,
				showHome: false
			}
		},
		provide(){
			return {
				isVip: true
			}
		},
		computed: mapGetters(['isLogin']),
		onLoad() {
			//订阅消息进入返回首页
			let pages = getCurrentPages();
			if(pages.length>1){
				this.showBack = true
				this.showHome = false
			}else{
				this.showBack = false
				this.showHome = true
			}
			if (this.isLogin) {
				this.onLunch();
			}
		},
		onReachBottom() {
			this.groomList();
		},
		onPageScroll(e) {
			uni.$emit('scroll');
			// #ifdef MP
			if (e.scrollTop > 50) {
				this.isScrolling = true;
				this.iconColor = '#333333';
			} else if (e.scrollTop < 50) {
				this.isScrolling = false;
				this.iconColor = '#FFFFFF';
			}
			// #endif
		},
		methods: {
			onLunch() {
				this.memberCard();
				this.groomList();
			},
			pay() {
				this.createMemberCard('weixin');
			},
			scrollToCard() {
				const query = uni.createSelectorQuery().in(this);
				query.select('#card').boundingClientRect(data => {
					uni.pageScrollTo({
						scrollTop: data.top
					});
				}).exec();
			},
			goDetail(id) {
				uni.navigateTo({
					url: `/pages/goods_details/index?id=${id}`
				});
			},
			// 付费会员数据
			memberCard() {
				uni.showLoading({
					title: '正在加载…'
				});
				memberCard().then(res => {
					uni.hideLoading();
					const {
						is_get_free,
						member_coupons,
						member_explain,
						member_rights,
						member_type
					} = res.data;
					this.isGetFree = is_get_free;
					this.userInfo = is_get_free.user_info;
					this.memberRights = member_rights;
					this.memberType = member_type;
					this.memberCoupons = member_coupons;
					this.memberExplain = member_explain;
					if (is_get_free.is_record) {
						this.memberType = this.memberType.filter(item => item.type !== 'free');
					};
					this.totalPrice = parseFloat(this.memberType[0].pre_price);
					this.type = this.memberType[0].type;
					this.totalPrice = parseFloat(this.memberType[0].pre_price);
					this.svip = this.memberType[0];
					memberOverdueTime({
						member_type: this.svip.type,
						vip_day: this.svip.vip_day
					}).then(res => {
						this.memberEndTime = res.data.data;
					});
				}).catch(err => {
					uni.showToast({
						title: err,
						icon: 'none'
					});
				});
			},
			groomList() {
				if (this.finished) {
					return;
				}
				uni.showLoading({
					title: '加载中'
				});
				groomList(5, {
					page: this.page,
					limit: this.limit
				}).then(res => {
					uni.hideLoading();
					this.goodsList = this.goodsList.concat(res.data.list);
					this.finished = res.data.list.length < this.limit;
					this.page += 1;
				}).catch(err => {
					uni.hideLoading();
				});
			},
			checkType(svip) {
				this.type = svip.type;
				this.svip = svip;
				this.totalPrice = parseFloat(svip.pre_price)
			},
			getMemberCard() {
				memberCard().then(res => {
					let data = res.data;
					this.memberType = data.member_type;
					this.memberRights = data.member_rights;
					this.isGetFree = data.is_get_free;
				}).catch(err => {

				});
			},
			memberSelect(index) {
				this.memberSelected = index;
			},
			// 卡密兑换
			drawMemberCard() {
				uni.navigateTo({
					url: '/pages/annex/vip_paid_active/index'
				})
			},
			// 立即购买
			createMemberCard(type) {
				uni.showLoading({
					title: '正在加载…'
				});
				let query = {
					pay_type: type || this.payMode[0].value,
					type: '1',
					// #ifdef H5
					from: this.isWeixin ? 'weixin' : 'weixinh5',
					// #endif
					// #ifdef MP
					from: 'routine',
					// #endif
					// #ifdef APP-PLUS
					from: 'weixin',
					// #endif
					member_type: this.svip.id,
					price: this.svip.pre_price,
					money: this.svip.price,
					// #ifdef H5
					quitUrl: location.port ? location.protocol + '//' + location.hostname + ':' + location.port +
						'/pages/annex/vip_paid/index' : location.protocol + '//' + location.hostname +
						'/pages/annex/vip_paid/index'
					// #endif
				};
				// #ifdef MP
				openPaySubscribe().then(() => {
					memberCardCreate(query).then(res => {
						// if (parseFloat(this.svip.pre_price) > 0) {
						// 	uni.hideLoading()
						// 	uni.reLaunch({
						// 		url: `/pages/goods/cashier/index?order_id=${res.data.order_id}&from_type=vip`
						// 	})
						// } else {
						// 	uni.hideLoading();
						// 	this.onLunch();
						// 	return this.$util.Tips({
						// 		title: '成功开启0元试用',
						// 	});
						// }
						uni.hideLoading()
						uni.redirectTo({
							url: `/pages/goods/cashier/index?order_id=${res.data.order_id}&from_type=vip`
						})
					}).catch(err => {
						uni.showToast({
							title: err
						});
					});
				});
				// #endif
				// #ifndef MP
				memberCardCreate(query).then(res => {
					uni.hideLoading()
					uni.redirectTo({
						url: `/pages/goods/cashier/index?order_id=${res.data.order_id}&from_type=vip`
					})
				}).catch(err => {
					uni.showToast({
						title: err,
						icon: 'none'
					});
				});
				// #endif
			},
			goRightDetail() {
				uni.navigateTo({
					url: `/pages/annex/vip_paid_rights/index`
				});
			},
		}
	}
</script>

<style lang="scss" scoped>
	.vipPaid {
		padding-bottom: 172rpx;
	}

	.headerBg {
		position: absolute;
		top: 0;
		left: 0;
		width: 100%;
		height: 820rpx;
		background: #121212;
		background-image: url("");
		background-position: right top;
		background-repeat: no-repeat;
		background-size: 566rpx 334rpx;
	}

	.card-section {
		position: relative;
		height: 528rpx;
		padding-top: 48rpx;
		border-bottom-right-radius: 48rpx;

		.vip-paid {
			position: absolute;
			top: 40rpx;
			right: 52rpx;
			width: 229rpx;
			height: 329rpx;
		}

		.expect {
			padding: 32rpx 0 58rpx 36rpx;

			.image {
				display: block;
				width: 414rpx;
				height: 76rpx;
			}

			.mark {
				display: inline-flex;
				align-items: center;
				height: 48rpx;
				padding: 0 24rpx;
				border-radius: 24rpx;
				margin-top: 12rpx;
				background: linear-gradient(-270deg, #E7B667 0%, #FFEAB5 100%);
				font-size: 28rpx;
				line-height: 48rpx;
				color: #7E4B06;

				.number {
					margin: 0 8rpx;
					font-family: SemiBold;
					font-size: 36rpx;
					color: var(--view-theme);
				}
			}
		}

		.wrapper {
			min-height: 450rpx;
			border-radius: 24rpx;
			margin: 0 20rpx;
			background: rgba(255, 255, 255, 0.05);
		}

		.avatar-wrap {
			min-height: 236rpx;
			padding: 62rpx 0 0 40rpx;

			.arrow {
				margin-top: 12rpx;
				font-size: 22rpx;
				line-height: 30rpx;
				color: rgba(254, 227, 174, 0.6);

				.iconfont {
					font-size: 24rpx;
				}
			}

			.text {
				flex: 1;
				margin-left: 15rpx;
				font-size: 22rpx;
				line-height: 30rpx;
				color: #89735B;

				.shopName {
					max-width: 164rpx;
				}

				.name {
					display: inline-block;
					vertical-align: middle;
					font-weight: 500;
					font-size: 32rpx;
					line-height: 44rpx;
					background: linear-gradient(-270deg, #FACB7A 0%, #FFF3D5 100%);
					-webkit-background-clip: text;
					-webkit-text-fill-color: transparent;
				}
			}
		}

		.avatar {
			width: 88rpx;
			height: 88rpx;
			border-radius: 50%;
			margin-right: 20rpx;
		}

		.tips {
			margin-top: 6rpx;
			font-size: 22rpx;
			line-height: 30rpx;
			color: rgba(254, 227, 174, 0.8);
		}

		.info {
			margin-top: 74rpx;
			font-size: 24rpx;
			color: #AE5A2A;
		}

		.num {
			font-size: 36rpx;
		}

		.btn {
			width: 146rpx;
			height: 44rpx;
			border-radius: 22rpx;
			background-color: #FFFFFF;
			line-height: 44rpx;
			text-align: center;
		}
	}

	.right-section {
		.section-hd {
			padding: 32rpx 32rpx 36rpx;
			font-weight: 500;
			font-size: 30rpx;
			line-height: 44rpx;
			color: rgba(250, 204, 125, 0.8);

			.arrow {
				font-weight: 400;
				font-size: 24rpx;
				line-height: 34rpx;
				color: rgba(255, 255, 255, 0.6);
			}

			.iconfont {
				font-size: 24rpx;
			}
		}

		.section-bd {
			padding: 0 0 32rpx;

			.item {
				flex: 1;
				min-width: 0;
			}

			.image-wrap {
				position: relative;
				width: 88rpx;
				height: 88rpx;
				border-radius: 50%;
				margin: 0 auto;

				.image-wrap-inner {
					display: flex;
					justify-content: center;
					align-items: center;
					width: 100%;
					height: 100%;
					border-radius: 50%;
				}
			}

			.image-inner {
				height: 100%;
				border-radius: 50%;
			}

			.image {
				width: 100%;
				height: 100%;
			}

			.text {
				margin-top: 28rpx;
				text-align: center;
				font-size: 20rpx;
				line-height: 28rpx;
				color: rgba(255, 255, 255, 0.4);
			}

			.name {
				margin-bottom: 8rpx;
				font-size: 26rpx;
				line-height: 37rpx;
				color: rgba(255, 255, 255, 0.7);
			}
		}
	}

	.other-section {
		position: relative;
		border-top-left-radius: 48rpx;
		border-top-right-radius: 48rpx;
		background-color: #F5F5F5;
	}

	.type-section {
		padding: 20rpx;

		.type-section-inner {
			border-radius: 24rpx;
			background-color: #FFFFFF;
		}

		.title {
			padding: 32rpx 32rpx 36rpx;
			border-radius: 24rpx 24rpx 0 0;
			background: linear-gradient(180deg, #FFEFCE 0%, #FFFFFF 100%);
			font-weight: bold;
			font-size: 32rpx;
			line-height: 44rpx;
			color: #333333;

			.bold {
				display: inline-block;
				margin-right: 14rpx;
				font-weight: bold;
				font-size: 32rpx;
				line-height: 45rpx;
				color: #333333;

				+view {
					display: inline-block;
				}
			}

			.time {
				margin-left: 14rpx;
				font-size: 24rpx;
				color: #AE5A2A;
			}

			.cash {
				font-weight: 400;
				font-size: 26rpx;
				line-height: 36rpx;
				color: rgba(217, 126, 29, 0.8);
			}
		}

		.scroll {
			white-space: nowrap;
		}

		.item {
			position: relative;
			display: inline-block;
			width: 220rpx;
			padding: 36rpx 0 32rpx;
			border: 1rpx solid #DDDDDD;
			border-radius: 24rpx;
			margin: 16rpx 24rpx 32rpx 0;

			&:first-child {
				margin: 16rpx 24rpx 32rpx 32rpx;
			}

			&:last-child {
				margin: 16rpx 32rpx 32rpx 0;
			}

			&.on {
				border: 2rpx solid #D97E1D;
				background: linear-gradient(180deg, #FFFFFF 0%, #FEF5E6 100%);
			}

			.badge {
				position: absolute;
				top: 0;
				left: 0;
				height: 44rpx;
				padding: 0 12rpx;
				border-radius: 22rpx 22rpx 22rpx 0;
				margin-top: -18rpx;
				background: linear-gradient(90deg, #FF7931 0%, #E93323 100%);
				font-size: 24rpx;
				line-height: 44rpx;
				color: #FFFFFF;
			}

			.name {
				text-align: center;
				font-weight: 500;
				font-size: 28rpx;
				line-height: 40rpx;
				color: #7E4B06;
			}

			.button {
				width: 142rpx;
				height: 52rpx;
				border-radius: 22rpx;
				margin: 18rpx auto 0;
				background-color: #FEE8BC;
				text-align: center;
				font-size: 22rpx;
				line-height: 52rpx;
				color: #333333;
			}
		}

		.new {
			margin-top: 16rpx;
			text-align: center;
			font-family: SemiBold;
			font-size: 36rpx;
			line-height: 62rpx;
			color: #333333;

			.num {
				// margin-left: 4rpx;
				font-size: 52rpx;
			}
		}

		.old {
			margin-top: 4rpx;
			text-decoration: line-through;
			text-align: center;
			font-size: 24rpx;
			line-height: 34rpx;
			color: #999999;
		}

		.info {
			margin-top: 4rpx;
			text-align: center;
			font-size: 24rpx;
			line-height: 34rpx;
			color: #999999;
		}

		.agree {
			font-size: 22rpx;
			text-align: center;
			color: #797979;

			.link {
				display: inline-block;
			}

			.mark {
				color: #AE5A2A;
			}
		}
	}

	.footer-placeholder {
		height: 152rpx;
		height: calc(152rpx + constant(safe-area-inset-bottom));
		height: calc(152rpx + env(safe-area-inset-bottom));
	}

	.footer {
		position: fixed;
		bottom: 0;
		left: 0;
		width: 100%;
		padding: 22rpx 20rpx;
		padding-bottom: calc(22rpx + constant(safe-area-inset-bottom));
		padding-bottom: calc(22rpx + env(safe-area-inset-bottom));
		background-color: #FFFFFF;

		.text {
			flex: 1;
			height: 108rpx;
			padding-left: 48rpx;
			border-radius: 54rpx 0 0 54rpx;
			background-color: #222222;
			font-size: 28rpx;
			color: #FACC7D;
		}

		.symbol {
			font-weight: 600;
			font-size: 40rpx;
		}

		.number {
			font-family: SemiBold;
			font-size: 56rpx;
		}

		.original {
			margin-left: 16rpx;
			text-decoration: line-through;
			font-size: 28rpx;
			color: rgba(255, 255, 255, 0.4);
		}

		.buy {
			position: relative;
			width: 266rpx;
			height: 108rpx;
			border-radius: 0 54rpx 54rpx 0;
			background: linear-gradient(to right, #FFDD9C, #EEA82F);
			font-weight: 500;
			font-size: 36rpx;
			color: #7E4B06;

			&::before {
				content: "";
				position: absolute;
				top: 0;
				left: 0;
				border-width: 54rpx 20rpx;
				border-style: solid;
				border-color: #222222 transparent transparent #222222;
			}
		}
	}

	.coupon-section {
		border-radius: 24rpx;
		margin: 0 20rpx;
		background-color: #FFFFFF;

		.section-hd {
			padding: 32rpx 32rpx 24rpx;
			font-weight: bold;
			font-size: 32rpx;
			line-height: 44rpx;
			color: #333333;

			.link {
				font-weight: 400;
				font-size: 26rpx;
				line-height: 36rpx;
				color: #666666;

				.iconfont {
					font-size: 24rpx;
				}
			}
		}

		.section-bd {
			padding-bottom: 24rpx;
			white-space: nowrap;

			.item {
				position: relative;
				display: inline-block;
				width: 151rpx;
				padding: 0 6rpx;
				border-radius: 24rpx;
				margin: 20rpx 14rpx 0 0;
				background-color: #E0A558;

				&:first-child {
					margin-left: 32rpx;
				}

				.inner {
					padding-bottom: 76rpx;
					border: 2rpx solid #FFEDCA;
					border-radius: 12rpx;
					margin-top: -20rpx;
					background-color: #FFFFFF;

					.type {
						width: 96rpx;
						height: 38rpx;
						border-radius: 0 0 12rpx 12rpx;
						background-color: rgba(224, 165, 88, 0.2);
						margin: 0 auto;
						text-align: center;
						font-size: 20rpx;
						line-height: 38rpx;
						color: #333333;
					}
				}

				&.gray {
					background-image: url("");

					.btn {
						color: #9F9F9F;
					}

					.money {
						color: #fff;

						.num {
							color: #fff;
						}
					}

					.name {
						color: #fff;
					}

					.text {
						color: #9F9F9F;
					}

					.mark {
						color: #A8A8A8;
					}
				}

				.btn {
					width: 52rpx;
					padding-right: 14rpx;
					padding-left: 14rpx;
					white-space: pre-wrap;
					font-size: 24rpx;
					color: #5D3324;
				}

				.text {
					position: absolute;
					bottom: 0;
					left: 0;
					width: 100%;
					height: 76rpx;
					padding-top: 30rpx;
					background-image: url("");
					background-repeat: no-repeat;
					background-size: 100% 100%;
				}

				.name {
					overflow: hidden;
					white-space: nowrap;
					text-overflow: ellipsis;
					font-weight: bold;
					font-size: 28rpx;
					line-height: 40rpx;
					color: #5D3324;
				}

				.mark {
					text-align: center;
					font-weight: 500;
					font-size: 22rpx;
					line-height: 30rpx;
					color: rgba(250, 204, 125, 0.8);
				}

				.money {
					margin-top: 16rpx;
					text-align: center;
					font-weight: 500;
					font-size: 28rpx;
					line-height: 44rpx;
					color: #E0A558;
				}

				.num {
					font-family: SemiBold;
					font-size: 44rpx;
				}
			}
		}
	}

	.recommend /deep/ .fw-500 {
		font-weight: bold;
	}
</style>